const banner_list = require('../models/banner')

// 获取banner
const getBanners = async (ctx) => {
  const query = ctx.request.query
  const data = await banner_list.getBanners(query)
  if(data) {
    ctx.body = {
      code: 0,
      data,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 根据id获取banner
const getBannerById = async (ctx) => {
  const id = ctx.params.id
  const data = await banner_list.getBannerById(id)
  if(data) {
    ctx.body = {
      code: 0,
      data,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 添加banner
const addBanner = async (ctx) => {
  const data = ctx.request.body
  const result = await banner_list.addBanner(data)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 编辑banner
const updateBanner = async (ctx) => {
  const id = ctx.params.id
  const data = ctx.request.body
  const result = await banner_list.updateBanner(id, data)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
module.exports = {
  getBanners,
  getBannerById,
  addBanner,
  updateBanner
}